import { observable, action, autorun, toJS } from 'mobx';
import { WeaTools, WeaLocaleProvider } from 'ecCom';
import { message, Modal } from 'antd';
import * as Apis from '../apis/highSeas';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export default class highSeasLogListStore {
    @observable tableStore = new TableStore();
    @observable loading = false;
    @observable searchConditionForm = new WeaForm();
    @observable searchConditionComponents = {};
    @observable showSearchAd = false;
    @observable searchBaseParams = {};
    @observable operateType = '';

    @action
    initDatas = (params = {}) => {
      this.searchBaseParams = {};
      this.tableStore = new TableStore();
      this.operateType = params.operateType;
      this.loading = true;
      this.searchConditionForm.reset();
      this.getTableData({ ...params, onlysessionkey: false });
    }

    @action
    doSearch = () => {
      this.tableStore = new TableStore();
      const that = this;
      this.loading = true;
      this.setShowSearchAd(false);
      const searchParamsAd = this.searchConditionForm.getFormParams();
      const requestParams = { ...searchParamsAd, operateType: this.operateType, onlysessionkey: true };
      for (const item in requestParams) {
        if (requestParams[item] == '') {
          delete requestParams[item];
        }
      }

      this.getTableData(requestParams);
    }

    getTableData = (params = {}) => {
      Apis.getHighSeasInfo('log', params).then((data) => {
        if (data.status == '0') {
          message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
        } else {
          this.tableStore.getDatas(data.datas, 1);
          if (!params.onlysessionkey) {
            this.searchConditionComponents = data.formItemGroupList;
            this.searchConditionForm.initFormFields(this.searchConditionComponents);
          }
        }
        this.loading = false;
      });
    }

    @action
    tableReload = () => {
      this.tableStore.getDatas(this.tableSessionKey, this.tableStore.current);
    }

    @action
    setShowSearchAd = (bool = false) => {
      this.showSearchAd = bool;
    }

    clearAdFields = () => {
      this.searchConditionForm.reset();
    }

    @action
    setSearchKey = (v = '') => {
      try {
        this.searchBaseParams.name = v;
        const searchKeyField = {};
        searchKeyField.name = v;
        this.searchConditionForm.updateFields(searchKeyField, false);
      } catch (error) {
        // 高级搜索中没有searchKey的情况
      }
    }

    @action
    searchConditionFieldChange = (fieldObj) => {
      if (fieldObj.name) {
        this.searchBaseParams = { ...this.searchBaseParams, name: fieldObj.name.value };
      }
    }
}
